
import UIKit

extension UIView {
    
    var x: CGFloat {
        get { return self.frame.origin.x }
        set { self.frame.origin.x = newValue }
    }
    
    var y: CGFloat {
        get { return self.frame.origin.y }
        set { self.frame.origin.y = newValue }
    }
    
    var width: CGFloat {
        get { return self.frame.size.width }
        set { self.frame.size.width = newValue }
    }
    
    var height: CGFloat {
        get { return self.frame.size.height }
        set { self.frame.size.height = newValue }
    }
    
    var centerX: CGFloat {
        get { return self.center.x }
        set { self.center.x = newValue }
    }
    
    var centerY: CGFloat {
        get { return self.center.y }
        set { self.center.y = newValue }
    }
    
    var top: CGFloat {
        get { return self.frame.origin.y }
        set { self.frame.origin.y = newValue }
    }
    
    var bottom: CGFloat {
        get { return self.frame.origin.y + self.frame.size.height }
        set { self.frame.origin.y = newValue - self.frame.size.height }
    }
    
    var size: CGSize {
        get { return self.frame.size }
        set { self.frame.size = newValue }
    }
    
    var maxX: CGFloat {
        return self.frame.origin.x + self.frame.size.width
    }
    
    var maxY: CGFloat {
        return self.frame.origin.y + self.frame.size.height
    }
    
    @discardableResult
    class func addTo(_ superView: UIView) -> Self {
        let view = Self.init()
        superView.addSubview(view)
        return view
        
    }
    
    @discardableResult
    func addTapGesturesWithTarget(target: Any?, action: Selector?) -> Self {
        self.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: target, action: action)
        self.addGestureRecognizer(tap)
        return self
    }
    
    @discardableResult
    func addSubviews(_ views: UIView...) -> Self {
        views.forEach { addSubview($0) }
        return self
    }
    
    @discardableResult
    func addCorner(radius: CGFloat) -> Self {
        layer.cornerRadius = radius
        layer.masksToBounds = true
        return self
    }
    
    
    func addRoundingCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path: UIBezierPath
        
        if radius > 0 {
            path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        } else {
            path = UIBezierPath(rect: self.bounds)
        }
        
        let shape = CAShapeLayer()
        shape.frame = self.bounds
        shape.path = path.cgPath
        self.layer.mask = shape
    }
    
    @discardableResult
    static func make<T: UIView>(_ config: (T) -> Void) -> T {
        let obj = T()
        config(obj)
        return obj
    }

}



